package com.zhan.ielts.web.controller.word;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.ielts.domain.WordIelts;
import com.zhan.ielts.domain.vo.word.WordIeltsVo;
import com.zhan.ielts.domain.word.Dictionary;
import com.zhan.ielts.service.word.DictionaryService;
import com.zhan.ielts.service.word.WordIeltsService;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Administrator on 2016/9/28.
 */
@Controller
@RequestMapping("/wordIelts")
public class WordIeltsController extends BaseControllerImpl<WordIelts, WordIelts> {
    @Autowired
    WordIeltsService wordIeltsService;

    @Autowired
    DictionaryService dictionaryService;

    @Override
    protected BaseService<WordIelts> getBaseService() {
        return wordIeltsService;
    }

//    @Override
//    @RequestMapping(method = RequestMethod.GET)
//    public ModelAndView selectList(WordIelts query, @PageableDefault Pageable pageable) {
//        ModelAndView mav = null;
//        try {
//            Page<WordIeltsVo> page = wordIeltsService.selectWordList(query, pageable);
//            mav = new ModelAndView("/word/wordIelts/listWordIelts", "page", page);
//            mav.addObject("query", query);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return mav;
//    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView selectList(WordIeltsVo query, @PageableDefault Pageable pageable) {
        ModelAndView mav = null;
        try {
            Page<WordIeltsVo> page = wordIeltsService.selectWordList(query, pageable);
            mav = new ModelAndView("/word/wordIelts/listWordIelts", "page", page);
            mav.addObject("query", query);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    @Override
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public ModelAndView editView(@PathVariable("id") String id) {
        Object obj = getBaseService().queryById(id);
        return new ModelAndView("/word/wordIelts/editWordIelts", path.getEntityName(), obj);
    }


    @Override
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addView() {
        return new ModelAndView("/word/wordIelts/addWordIelts");
    }

    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    public ModelAndView addOne(WordIeltsVo entity) {
        Dictionary dictionary = new Dictionary();
        if (TextUtils.isEmpty(entity.getWord())) {
            return new ModelAndView("/common/error");
        }
        dictionary.setWord(entity.getWord());
        dictionary = dictionaryService.queryOne(dictionary);
        if (null == dictionary) {
            ModelAndView modelAndView = new ModelAndView("/common/error");
            String tips = "该单词 " + entity.getWord() + " 在单词表里不存在！";
            modelAndView.addObject("tips", tips);
            return modelAndView;
//            throw new IllegalArgumentException("该单词" + entity.getWord() + "在单词表里不存在！");
        }
        entity.setDictionaryId(dictionary.getId());
        WordIelts wordIelts = getBaseService().queryOne(entity);
        if (wordIelts != null) {
            throw new IllegalArgumentException("该单词已在雅思单词表里已存在!");
        }
        getBaseService().insert(entity);
        return new ModelAndView("redirect:/wordIelts");
    }


    @RequestMapping(value = "/updateOne", method = RequestMethod.POST)
    public ModelAndView editWord(WordIelts query) {
        if (TextUtils.isEmpty(query.getId())) {
            getBaseService().insert(query);
        } else {
            getBaseService().updateByIdSelective(query);
        }
        return new ModelAndView("redirect:/wordIelts");
    }
}
